﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Microsoft.International.Converters.PinYinConverter;

namespace MyConsoleApp.Code
{
    public sealed class PinYin
    {
        private PinYin() { }

        /// <summary>
        /// 获取首拼或全拼。大写
        /// </summary>
        /// <param name="chineseString"></param>
        /// <param name="isFull">true为全拼，false为首拼</param>
        /// <returns></returns>
        public static string GetPY(string chineseString, bool isFull)
        {
            StringBuilder sb = new StringBuilder(50);
            foreach (var item in chineseString.ToCharArray())
            {
                if (!ChineseChar.IsValidChar(item))
                {
                    sb.Append(item);
                }
                else
                {
                    System.Collections.ObjectModel.ReadOnlyCollection<string> py = new ChineseChar(item).Pinyins;
                    if (py.Count > 0 && py[0].Length > 0)
                    {
                        if (isFull)
                            sb.Append(py[0].Substring(0, py[0].Length - 1));
                        else
                            sb.Append(py[0][0]);
                    }
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获取首拼(多音字"|"分隔)和全拼
        /// </summary>
        /// <param name="chineseString"></param>
        /// <param name="pyStr"></param>
        /// <param name="pyFullStr"></param>
        public static void GetHomoPhonePY(string chineseString, out string pyStr, out string pyFullStr)
        {
            pyStr = GetHomoPhonePY(chineseString);
            pyFullStr = GetPY(chineseString, true);
        }

        /// <summary>
        /// 获取多音字首拼(多音字"|"分隔)
        /// 鼻咽癌SIEMENS首程->BYASIEMENSSC
        /// </summary>
        /// <param name="chineseString"></param>
        /// <returns></returns>
        public static string GetHomoPhonePY(string chineseString)
        {
            StringBuilder sb = new StringBuilder(50);

            char[] chineseCharArray = chineseString.ToCharArray();
            Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>();

            for (int i = 0; i < chineseCharArray.Length; i++)
            {
                char item = chineseCharArray[i];
                if (!ChineseChar.IsValidChar(item))
                {
                    dic.Add(i, new List<string> { item.ToString() });
                }
                else
                {
                    ChineseChar chinaChar = new ChineseChar(item);
                    ReadOnlyCollection<string> py = chinaChar.Pinyins;
                    if (py.Count > 0 && py[0].Length > 0)
                    {
                        dic.Add(i, py.Where(p => !string.IsNullOrEmpty(p)).Select(p => p[0].ToString()).Distinct().ToList());
                    }
                }
            }

            OutPutHomoPhone(dic, 0, "", ref sb);

            return sb.ToString();
        }

        /// <summary>
        /// 递归出多音字的首拼
        /// </summary>
        /// <param name="dic">多音字字典</param>
        /// <param name="key">字典下标</param>
        /// <param name="parentValue">递归的首拼</param>
        /// <param name="sb">结果对象</param>
        private static void OutPutHomoPhone(
            Dictionary<int, List<string>> dic,
            int key,
            string parentValue,
            ref StringBuilder sb)
        {
            if (dic.Keys.Contains(key))
            {
                foreach (string x in dic[key])
                {
                    if (dic.Keys.Count - 1 >= key)
                    {
                        string outString = string.Format("{0}{1}", parentValue, x);
                        if (outString.Length == dic.Keys.Count)
                        {
                            sb.AppendFormat("{0}|", outString);
                        }
                        OutPutHomoPhone(dic, key + 1, outString, ref sb);
                    }
                }
            }
        }

    }
}
